# -*- coding: utf-8 -*-

from django import forms
from timelineapp.models import TimelineCategory, Timeline
from eventapp.models import Asset, AssetType, Event
from django.contrib.auth.models import User
from datetime import datetime

def timeline_selector():
    timeline_categories =  TimelineCategory.objects.order_by('name')
    categories_choices = []
    for i in timeline_categories:
        categories_choices.append((i.id, i.name))
    categories_choices = tuple(categories_choices)
    return categories_choices

class TimelineForm(forms.Form):
    title = forms.CharField(max_length=100, label="Titulo")
    description = forms.CharField(required=False, label="Descricao", widget=forms.Textarea) 
    category = forms.ChoiceField(timeline_selector(), label="Categoria")
    initial_date = forms.DateField(required=False, label="Data de Inicio", input_formats=['%d/%m/%Y'], widget=forms.DateInput)
    final_date = forms.DateField(required=False, label="Data Final", input_formats=['%d/%m/%Y'], widget=forms.DateInput)
    private = forms.BooleanField(initial=True, required=False, label="Timeline Privada")
    invitations = forms.CharField(required=False, label="Convites", widget=forms.Textarea)
    
    cover = forms.CharField(max_length=100, label="Capa", required=False)
    caption = forms.CharField(max_length=100, label="Legenda", required=False)
    credit = forms.CharField(max_length=100, label="Crédito", required=False)
    
    def save(self, request):
        cd = self.cleaned_data
            
        is_private = 0
        if (cd["private"]):
            is_private = 1
            
        new_timeline = Timeline(title = cd["title"],
                privacy = is_private,
                creation_date = datetime.now(),
                initial_date = cd["initial_date"],
                final_date = cd["final_date"],
                description = cd["description"])
                        
        user = User.objects.get(id=request.user.id)
        new_timeline.creator = user
        new_timeline.save()

        category = TimelineCategory.objects.get(id=cd["category"])
        new_timeline.categories.add(category)
        
        # create cover event
        edate = cd["initial_date"]
        if not edate:
            edate = datetime.now() 
            
        cover_event = Event(title = cd["title"],
                  creation_date = datetime.now(),
                  date = edate,
                  description = cd["description"],
                  creator = user,
                  approved = 1)
        cover_event.save()
        
        new_timeline.cover = cover_event
        new_timeline.events.add(cover_event)
        new_timeline.save()
        
        # create cover asset
        cover_asset = Asset(media = cd["cover"],
                            caption = cd["caption"],
                            credit = cd["credit"], 
                            creator = user,
                            creation_date = datetime.now(),
                            approved = 1,
                            asset_type = AssetType.objects.get(pk=8))
        cover_asset.save()
        cover_event.assets.add(cover_asset)
        
        return new_timeline